package cn.stylefeng.guns.modular.demos.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.demos.entity.EduCourseSelectionInfo;
import cn.stylefeng.guns.modular.demos.mapper.EduCourseSelectionInfoMapper;
import cn.stylefeng.guns.modular.demos.model.params.EduCourseSelectionInfoParam;
import cn.stylefeng.guns.modular.demos.model.result.*;
import cn.stylefeng.guns.modular.demos.service.EduCourseSelectionInfoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 选课信息表 服务实现类
 * </p>
 *
 * @author Frey Wang
 * @since 2022-11-09
 */
@Service
public class EduCourseSelectionInfoServiceImpl extends ServiceImpl<EduCourseSelectionInfoMapper, EduCourseSelectionInfo> implements EduCourseSelectionInfoService {

    @Autowired
    private EduCourseSelectionInfoMapper eduCourseSelectionInfoMapper;

    @Override
    public void add(EduCourseSelectionInfoParam param){
        EduCourseSelectionInfo entity = getEntity(param);
        entity.setStatus(EduCourseSelectionInfo.Status.NO_SIGN.getValue());
        this.save(entity);
    }

    @Override
    public void delete(EduCourseSelectionInfoParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(EduCourseSelectionInfoParam param){
        EduCourseSelectionInfo oldEntity = getOldEntity(param);
        EduCourseSelectionInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public EduCourseSelectionInfoResult findBySpec(EduCourseSelectionInfoParam param){
        return null;
    }

    @Override
    public List<EduCourseSelectionInfoResult> findListBySpec(EduCourseSelectionInfoParam param){
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(EduCourseSelectionInfoParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findSignPageBySpec(EduCourseSelectionInfoParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customSignPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(EduCourseSelectionInfoParam param){
        return param.getUniqueKey();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private EduCourseSelectionInfo getOldEntity(EduCourseSelectionInfoParam param) {
        return this.getById(getKey(param));
    }

    private EduCourseSelectionInfo getEntity(EduCourseSelectionInfoParam param) {
        EduCourseSelectionInfo entity = new EduCourseSelectionInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public BarChartRsp barChart() {
        List<BarChartResult> barChartResults = eduCourseSelectionInfoMapper.barChart();
        String[] x = {"","","","",""};
        Integer[] y = {0,0,0,0,0};
        if(CollectionUtils.isNotEmpty(barChartResults)){
            for(int i=0;i<barChartResults.size();i++){
                x[i] = barChartResults.get(i).getStudentName();
                y[i] = barChartResults.get(i).getCount();
            }
        }
        BarChartRsp barChartRsp = new BarChartRsp();
        barChartRsp.setX(x);
        barChartRsp.setY(y);
        return barChartRsp;
    }

    @Override
    public LineChartResult lineChart() {
        List<LineChartRsp> lineChartRsps = eduCourseSelectionInfoMapper.lineChart();
        //按日期分组
        Map<String,List<LineChartRsp>> timeLineChartMaps = lineChartRsps.stream()
                .collect(Collectors.groupingBy(lineChartRsp -> lineChartRsp.getDate()));
        LineChartResult lineChartResult = new LineChartResult();
        int i = 0;
        String[] x = new String[timeLineChartMaps.size()];
        Integer[] sumA = new Integer[timeLineChartMaps.size()];
        Integer[] menA = new Integer[timeLineChartMaps.size()];
        Integer[] womenA = new Integer[timeLineChartMaps.size()];
        for(Map.Entry<String,List<LineChartRsp>> entry : timeLineChartMaps.entrySet()){
            String time = entry.getKey();
            x[i] = time;
            List<LineChartRsp> list = entry.getValue();
            int total = 0,sum = 0,men = 0, women = 0;
            for(LineChartRsp lineChartRsp : list){
                if("总人数".equals(lineChartRsp.getName())){
                    total = lineChartRsp.getCount();
                    break;
                }
            }
            for(LineChartRsp lineChartRsp : list){
                if("签到总数".equals(lineChartRsp.getName())){
                    if(lineChartRsp.getCount()>0){
                        sum = (int) (((double)lineChartRsp.getCount()/(double) total)*100);
                    }else{
                        sum = 0;
                    }
                }
                if("男".equals(lineChartRsp.getName())){
                    if(lineChartRsp.getCount()>0){
                        men = (int) (((double)lineChartRsp.getCount()/(double) total)*100);
                    }else{
                        men = 0;
                    }
                }
                if("女".equals(lineChartRsp.getName())){
                    if(lineChartRsp.getCount()>0){
                        women = (int) (((double)lineChartRsp.getCount()/(double) total)*100);
                    }else{
                        women = 0;
                    }
                }
            }

            sumA[i] = sum;
            menA[i] = men;
            womenA[i] = women;

            i = i+1;
        }
        lineChartResult.setX(x);
        lineChartResult.setSum(sumA);
        lineChartResult.setMen(menA);
        lineChartResult.setWomen(womenA);
        return lineChartResult;
    }
}
